﻿using NDK.Motion.MachineManager;
using LightController;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace NDK.Motion.Frm
{
    public partial class Dlg_AddImageLights : Form
    {
        private ImageLights _imageLights = new ImageLights();
        public Dlg_AddImageLights()
        {
            InitializeComponent();
        }

        public ImageLights ImageLights
        {
            get { return _imageLights; }
            set { _imageLights = value; }
        }

        private void btn_Confirm_BtnClick(object sender, EventArgs e)
        {
            //图片名称不能为空
            if (string.IsNullOrEmpty(txt_ImageName.Text.Trim()))
            {
                lbl_ImgNameTip.ForeColor = Color.Red;
                txt_ImageName.Focus();
                return;
            }

            _imageLights.ImageName = txt_ImageName.Text.Trim();

            string Lights = txt_Lights.Text.Trim();
            if (Lights.Contains(","))
            {
                List<string> lightNameList = Lights.Split(',').ToList();
                foreach (var lightName in lightNameList)
                {
                    Light light = MachineDataManager.Instance.LightList.SingleOrDefault(l => l.Name == lightName);
                    if (light != null)
                        _imageLights.LightList.Add(new Light(light));
                }
            }
            else
            {
                Light light = MachineDataManager.Instance.LightList.SingleOrDefault(l => l.Name == Lights);
                if (light != null)
                    _imageLights.LightList.Add(new Light(light));
            }

            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void Dlg_AddLight_Load(object sender, EventArgs e)
        {
            cb_LightName.DisplayMember = "Name";
            cb_LightName.DataSource = new BindingList<Light>(MachineDataManager.Instance.LightList);
        }

        private void cb_LightControllerName_SelectedIndexChanged(object sender, EventArgs e)
        {
            string LightName = cb_LightName.Text.Trim();
            if (!txt_Lights.Text.Contains(LightName))
            {
                if (string.IsNullOrEmpty(txt_Lights.Text))
                {
                    txt_Lights.Text = LightName;
                }
                else
                {
                    txt_Lights.Text += ("," + LightName);
                }
            }
        }
    }
}
